package cn.formaggie.service.impl;

import cn.formaggie.dao.PermissionDao;
import cn.formaggie.entity.model.roleandpermission.Permission;
import cn.formaggie.entity.utilModel.PermissionIdWithBool;
import cn.formaggie.service.face.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("permissionService")
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Override
    public List<Permission> getPermissionsByRoleId(Integer roleId) {
        return permissionDao.getPermissionsByRoleId(roleId);
    }

    @Override
    public List<Permission> getPermissions() {
        return permissionDao.getPermissions();
    }

    @Override
    public Map<String, List<Permission>> getPermissionsOrderByType() {
        Map<String,List<Permission>> typeAndPermissions=new HashMap<>();
        List<Permission> permissions=permissionDao.getPermissions();
        List<Integer> indexs=findTypeIndex(permissions);

        for(int i=0;i<indexs.size();i++){
            String typeName=permissions.get(indexs.get(i)).getPermissionType().getPermissionType();
            List<Permission> permissionList=new ArrayList<>();
            int start=indexs.get(i);
            int end=indexs.get((i+1)<(indexs.size()-1)?(i+1):indexs.size()-1);
            if(i==indexs.size()-1){
                end=permissions.size();
            }
            for(int pIndex=start;pIndex<end;pIndex++){
                permissionList.add(permissions.get(pIndex));
            }
            typeAndPermissions.put(typeName,permissionList);
        }
        return typeAndPermissions;
    }

    @Override
    public Map<String, List<PermissionIdWithBool>> getPermissionsAndRoleSelected(Map<String, List<Permission>> all, Map<String, List<Permission>> permissionsOfRole) {
        Map<String, List<PermissionIdWithBool>> result=new HashMap<>();
        //初始化result
        for(String e:all.keySet()){
            List<PermissionIdWithBool> permissionIdWithBools=new ArrayList<>();
            for(Permission p:all.get(e)){
                permissionIdWithBools.add(new PermissionIdWithBool(p.getId(),p.getPermission(),false));
            }
            result.put(e,permissionIdWithBools);
        }
        if(permissionsOfRole==null){
            return result;
        }
        //分拣已经拥有的权限
        for(String e:permissionsOfRole.keySet()){
            List<PermissionIdWithBool> permissionIdWithBools=result.get(e);
            for(Permission p:permissionsOfRole.get(e)){
                for(PermissionIdWithBool model:permissionIdWithBools){
                    if(model.getPermissionId()==p.getId()){
                        model.setChecked(true);
                    }
                }
            }
        }
        return result;
    }

    private List<Integer> findTypeIndex(List<Permission> permissions){
        List<Integer> indexs=new ArrayList<>();
        boolean isTypeChange=true;
        String typeName=permissions.get(0).getPermissionType().getPermissionType();
        indexs.add(0);
        for(int i=0;i<permissions.size();i++){
            String tempName=permissions.get(i).getPermissionType().getPermissionType();
            if(tempName.equals(typeName)){
                continue;
            }else{
                indexs.add(i);
                typeName=tempName;
            }
        }
        return indexs;
    }



}
